package com.meirengu.webview.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.utils.crypto.HCCryptAES;
import com.meirengu.webview.common.ConfigConsts;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwCyphPush;
import com.meirengu.webview.model.KwCyphPushLog;
import com.meirengu.webview.service.KwCyphPushLogService;
import com.meirengu.webview.service.KwCyphPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.*;

import static com.meirengu.utils.HexUtil.toHexString;
import static com.meirengu.utils.MD5Util.md5Hex;
import static com.meirengu.utils.StringUtil.createNonceStr;
import static com.meirengu.webview.common.ConfigConsts.*;
import static com.meirengu.webview.common.Constants.*;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.http.HttpStatus.SC_OK;

/**
 * KwCyphPush服务实现层
 *
 * @author Mao
 * @create Tue May 08 11:07:30 CST 2018
 */
@Service
public class KwCyphPushServiceImpl extends BaseServiceImpl<KwCyphPush> implements KwCyphPushService {

    Logger logger = LoggerFactory.getLogger(KwCyphPushServiceImpl.class);

    @Autowired
    private KwCyphPushLogService kwCyphPushLogService;

    /**
     * 贷款
     *
     * @param
     * @return
     */

    public Integer applyLoan(String name, String mobile, String ip, String receivedChannel,
                             String sex, String birth, String city, String income, String loanAmount,
                             String age, String hasCreditCard, String hasHouse, String hasHousingLoan,
                             String hasCar, String hasCarLoan, String hasLifeInsurance, String insurancePay,
                             String welfare, String houseFund, String applyTime) throws Exception {

        String url = ConfigUtil.getConfig("changyuan.puhui.url");
        String channelNo = ConfigUtil.getConfig("changyuan.puhui.channelno");
        String key = ConfigUtil.getConfig("changyuan.puhui.key");


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channelNo", channelNo);
        jsonObject.put("name", name);
        jsonObject.put("mobile", mobile);
        jsonObject.put("ip", ip);
        jsonObject.put("receivedChannel", receivedChannel);
        /*params.put("idCard", idCard);*/
        jsonObject.put("sex", sex);
        jsonObject.put("birth", birth);
        jsonObject.put("city", city);
        jsonObject.put("income", income);
        jsonObject.put("loanAmount", loanAmount);
        /*params.put("entryDate", entryDate);*/
        jsonObject.put("age", age);
        jsonObject.put("hasCreditCard", hasCreditCard);
        jsonObject.put("hasHouse", hasHouse);
        jsonObject.put("hasHousingLoan", hasHousingLoan);
        jsonObject.put("hasCar", hasCar);
        jsonObject.put("hasCarLoan", hasCarLoan);
        jsonObject.put("hasLifeInsurance", hasLifeInsurance);
        jsonObject.put("insurancePay", insurancePay);
        jsonObject.put("welfare", welfare);
        jsonObject.put("houseFund", houseFund);
        jsonObject.put("applyTime", applyTime);
        jsonObject.put("sign", MD5Util.get32MD5(channelNo + name + mobile + key + applyTime));


        //接口调用信息表
        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode("cyph");
        kwCyphPush.setInterfaceName("畅愿普惠");
        kwCyphPush.setMobile(mobile);
        kwCyphPush.setCode(BUSINESS_STATUS_FAIL);


        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode("cyph");
        kwCyphPushLog.setInterfaceName("畅愿普惠");
        kwCyphPushLog.setMobile(mobile);
        kwCyphPushLog.setSendUrl(url.toString());
        kwCyphPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(200);
                kwCyphPushLog.setHttpContent(hr.toString());

                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("err");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage("成功");
                } else if ("1".equals(rCode)) {
                    if ("手机号存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("名单超量".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("缺少参数".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("2".equals(rCode)) {
                    if ("is a mobile black".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("系统异常，请联系开发人员解决！[500]".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("网络异常".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_NETWORK_ANOMALY);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("该手机号在半年之内已领取过免费险".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("422".equals(rCode)) {
                    if ("年龄不符合, 22和50之间".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("用户已经存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                }
            } else {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(code);
                kwCyphPushLog.setHttpContent(hr.toString());
            }
        } catch (Exception e) {
            kwCyphPushLog.setHttpCode(408);
            kwCyphPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwCyphPush);
            kwCyphPushLog.setPushId(kwCyphPush.getId());
            kwCyphPushLogService.insert(kwCyphPushLog);
            return kwCyphPush.getCode();
        }
    }


    /**
     * 保险
     *
     * @param
     * @return
     */

    public Integer applyInsurance(String name, String mobile, String ip, String sex, String birth,
                                  String city, String income, String hasHouse, String hasCar,
                                  String hasLifeInsurance, String applyTime, String userAgent) throws Exception {

        String url = ConfigUtil.getConfig("changyuan.insure.url");
        String channelNo = ConfigUtil.getConfig("changyuan.insure.channelno");
        String key = ConfigUtil.getConfig("changyuan.insure.key");


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channelNo", channelNo);
        jsonObject.put("name", name);
        jsonObject.put("mobile", mobile);
        jsonObject.put("sex", sex);
        jsonObject.put("birth", birth);
        jsonObject.put("city", city);
        jsonObject.put("ip", ip);
        jsonObject.put("hasCar", hasCar);
        jsonObject.put("hasHouse", hasHouse);
        jsonObject.put("hasInsurance", hasLifeInsurance);
        jsonObject.put("income", income);
        jsonObject.put("userAgent", userAgent);
        jsonObject.put("applyTime", applyTime);
        jsonObject.put("sign", MD5Util.get32MD5(channelNo + name + mobile + applyTime + key));


        //接口调用信息表
        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode("cyis");
        kwCyphPush.setInterfaceName("畅愿保险");
        kwCyphPush.setMobile(mobile);
        kwCyphPush.setCode(BUSINESS_STATUS_FAIL);


        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode("cyis");
        kwCyphPushLog.setInterfaceName("畅愿保险");
        kwCyphPushLog.setMobile(mobile);
        kwCyphPushLog.setSendUrl(url.toString());
        kwCyphPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(200);
                kwCyphPushLog.setHttpContent(hr.toString());

                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("err");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage("成功");
                } else if ("1".equals(rCode)) {
                    if ("手机号存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("名单超量".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("缺少参数".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("2".equals(rCode)) {
                    if ("is a mobile black".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("系统异常，请联系开发人员解决！[500]".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("网络异常".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_NETWORK_ANOMALY);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("该手机号在半年之内已领取过免费险".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if (rMsg.contains("mobile") && rMsg.contains("error")) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:手机号格式不符合");
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("422".equals(rCode)) {
                    if ("年龄不符合, 22和50之间".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if (rMsg.contains("用户已经存在")) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + "用户已存在");
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                }
            } else {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(code);
                kwCyphPushLog.setHttpContent(hr.toString());
            }
        } catch (Exception e) {
            kwCyphPushLog.setHttpCode(408);
            kwCyphPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwCyphPush);
            kwCyphPushLog.setPushId(kwCyphPush.getId());
            kwCyphPushLogService.insert(kwCyphPushLog);
            return kwCyphPush.getCode();
        }


    }

    public Integer applyLoan2(String name, String mobile, String ip, String receivedChannel,
                              String sex, String birth, String city, String income, String loanAmount,
                              String age, String hasCreditCard, String hasHouse, String hasHousingLoan,
                              String hasCar, String hasCarLoan, String hasLifeInsurance, String insurancePay,
                              String welfare, String houseFund, String applyTime) throws Exception {

        String url = ConfigUtil.getConfig("changyuan.puhui2.url");
        String channelNo = ConfigUtil.getConfig("changyuan.puhui2.channelno");
        String key = ConfigUtil.getConfig("changyuan.puhui2.key");


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channelNo", channelNo);
        jsonObject.put("name", name);
        jsonObject.put("mobile", mobile);
        jsonObject.put("ip", ip);
        jsonObject.put("receivedChannel", receivedChannel);
        /*params.put("idCard", idCard);*/
        jsonObject.put("sex", sex);
        jsonObject.put("birth", birth);
        jsonObject.put("city", city);
        jsonObject.put("income", income);
        jsonObject.put("loanAmount", loanAmount);
        /*params.put("entryDate", entryDate);*/
        jsonObject.put("age", age);
        jsonObject.put("hasCreditCard", hasCreditCard);
        jsonObject.put("hasHouse", hasHouse);
        jsonObject.put("hasHousingLoan", hasHousingLoan);
        jsonObject.put("hasCar", hasCar);
        jsonObject.put("hasCarLoan", hasCarLoan);
        jsonObject.put("hasLifeInsurance", hasLifeInsurance);
        jsonObject.put("insurancePay", insurancePay);
        jsonObject.put("welfare", welfare);
        jsonObject.put("houseFund", houseFund);
        jsonObject.put("applyTime", applyTime);
        jsonObject.put("sign", MD5Util.get32MD5(channelNo + name + mobile + key + applyTime));


        //接口调用信息表
        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode("cyph2");
        kwCyphPush.setInterfaceName("畅愿普惠2");
        kwCyphPush.setMobile(mobile);
        kwCyphPush.setCode(BUSINESS_STATUS_FAIL);


        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode("cyph2");
        kwCyphPushLog.setInterfaceName("畅愿普惠2");
        kwCyphPushLog.setMobile(mobile);
        kwCyphPushLog.setSendUrl(url.toString());
        kwCyphPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(200);
                kwCyphPushLog.setHttpContent(hr.toString());

                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("err");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage("成功");
                } else if ("1".equals(rCode)) {
                    if ("手机号存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("名单超量".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("缺少参数".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("2".equals(rCode)) {
                    if ("is a mobile black".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("系统异常，请联系开发人员解决！[500]".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("网络异常".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_NETWORK_ANOMALY);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("该手机号在半年之内已领取过免费险".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("422".equals(rCode)) {
                    if ("年龄不符合, 22和50之间".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("用户已经存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                }
            } else {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(code);
                kwCyphPushLog.setHttpContent(hr.toString());
            }
        } catch (Exception e) {
            kwCyphPushLog.setHttpCode(408);
            kwCyphPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwCyphPush);
            kwCyphPushLog.setPushId(kwCyphPush.getId());
            kwCyphPushLogService.insert(kwCyphPushLog);
            return kwCyphPush.getCode();
        }
    }

    @Override
    public Integer applyLoanSun(String name, String phone, String gender, String age, String birthday,
                                String applyDate, String salary, String salaryType, String loanRecords,
                                String hasCreditCard, String hasHouse, String hasHouseLoan, String hasHouseFund,
                                String hasCar, String hasCarLoan, String hasSocialSecurity, String hasInsurance,
                                String hasLoan, Integer loanTime, String city) throws Exception {

        String url = ConfigUtil.getConfig("changyuan.sun.url");
        String channel = ConfigUtil.getConfig("changyuan.sun.channelno");
        String key = ConfigUtil.getConfig("changyuan.sun.key");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channel", channel);
        jsonObject.put("name", name);
        jsonObject.put("phone", phone);
        jsonObject.put("gender", gender);
        //params.put("idCard", idCard);
        jsonObject.put("age", age);
        jsonObject.put("birthday", birthday);
        jsonObject.put("applyDate", applyDate);
        jsonObject.put("salary", salary);
        jsonObject.put("loanRecords", loanRecords);
        //params.put("entryDate", entryDate);
        jsonObject.put("salaryType", salaryType);
        jsonObject.put("hasCreditCard", hasCreditCard);
        jsonObject.put("hasHouse", hasHouse);
        jsonObject.put("hasHouseLoan", hasHouseLoan);
        jsonObject.put("hasCar", hasCar);
        jsonObject.put("hasCarLoan", hasCarLoan);
        jsonObject.put("hasHouseFund", hasHouseFund);
        jsonObject.put("hasSocialSecurity", hasSocialSecurity);
        jsonObject.put("hasInsurance", hasInsurance);
        jsonObject.put("hasLoan", hasLoan);
        jsonObject.put("city", city);
        jsonObject.put("sign", MD5Util.MD5("channel=" + channel + "name=" + name + "phone=" + phone + "applydate=" + applyDate + key, "utf-8"));


        //接口调用信息表
        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode("cyyg");
        kwCyphPush.setInterfaceName("畅愿阳光");
        kwCyphPush.setMobile(phone);
        kwCyphPush.setCode(BUSINESS_STATUS_FAIL);


        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode("cyyg");
        kwCyphPushLog.setInterfaceName("畅愿阳光");
        kwCyphPushLog.setMobile(phone);
        kwCyphPushLog.setSendUrl(url.toString());
        kwCyphPushLog.setParams(jsonObject.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url, jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(200);
                kwCyphPushLog.setHttpContent(hr.toString());

                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("err");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage("成功");
                } else if ("1".equals(rCode)) {
                    if ("手机号存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("名单超量".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("缺少参数".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("2".equals(rCode)) {
                    if ("is a mobile black".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("系统异常，请联系开发人员解决！[500]".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("网络异常".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_NETWORK_ANOMALY);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("该手机号在半年之内已领取过免费险".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else if ("422".equals(rCode)) {
                    if ("年龄不符合, 22和50之间".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_BELIE);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else if ("用户已经存在".equals(rMsg)) {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    } else {
                        kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    }
                } else {
                    kwCyphPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                }
            } else {
                kwCyphPushLog.setBackUrl(hr.toString());
                kwCyphPushLog.setHttpCode(code);
                kwCyphPushLog.setHttpContent(hr.toString());
            }
        } catch (Exception e) {
            kwCyphPushLog.setHttpCode(408);
            kwCyphPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwCyphPush);
            kwCyphPushLog.setPushId(kwCyphPush.getId());
            kwCyphPushLogService.insert(kwCyphPushLog);
            return kwCyphPush.getCode();
        }
    }

    private byte[] key_kaisen = hexStr2Bytes("60CA0469A02C49C1BE0C37FDD15B97AB");
    private byte[] iv_kaisen = hexStr2Bytes("A2B9438A3E1D48C8A457311D68FB2408");
    private final String key_sign_kaisen = "B55C0AE7A9FC445D978C0EE9F71F99CD";

    @Override
    public Integer applyKaisen(String name, String idCard, String mobile, String city, String sex, String birthday, String province
            , String remark1, String remark2, String ip, String ipAddress, String userAgent, String remark3
            , String remark4, String remark5) throws Exception {
        String url = ConfigUtil.getConfig("kaisen.url");

        JSONObject paramJsonObject = new JSONObject();
        paramJsonObject.put("name", name);
        paramJsonObject.put("idno", idCard);
        paramJsonObject.put("mobile", mobile);
        paramJsonObject.put("city", city);
        paramJsonObject.put("sex", sex);
        paramJsonObject.put("birthday", birthday);
        paramJsonObject.put("province", province);
        paramJsonObject.put("remark1", remark1);
        paramJsonObject.put("remark2", remark2);
        paramJsonObject.put("ip", ip);
        paramJsonObject.put("ipAddress", ipAddress);
        paramJsonObject.put("userAgent", userAgent);
        paramJsonObject.put("remark3", remark3);
        paramJsonObject.put("remark4", remark4);
        paramJsonObject.put("remark5", remark5);

        String data = encrypt(paramJsonObject.toJSONString(), key_kaisen, iv_kaisen);

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("data", data);
        paramMap.put("uid", "XB-LA-190327");
        paramMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
        paramMap.put("nonce", createNonceStr(20));
        String sign = signParams(paramMap, key_sign_kaisen).toLowerCase();
        paramMap.put("signature", sign);

        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode(ConfigConsts.CHANNEL_CODE_KAISEN);
        kwCyphPush.setInterfaceName(ConfigConsts.CHANNEL_NAME_KAISEN);
        kwCyphPush.setMobile(mobile);

        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode(ConfigConsts.CHANNEL_CODE_KAISEN);
        kwCyphPushLog.setInterfaceName(ConfigConsts.CHANNEL_NAME_KAISEN);
        kwCyphPushLog.setMobile(mobile);
        kwCyphPushLog.setSendUrl(url);
        kwCyphPushLog.setParams(paramMap.toString());

        HttpUtil.HttpResult hr = HttpUtil.doPost2(url, paramMap);
        int statusCode = hr.getStatusCode();
        if (statusCode == 200) {
            kwCyphPushLog.setBackUrl(hr.toString());
            kwCyphPushLog.setHttpCode(200);
            kwCyphPushLog.setHttpContent(hr.getContent());

            String content = hr.getContent();
            if (!isEmpty(content)) {
                JSONObject jsonObject = (JSONObject) JSONObject.parse(content);

                String errno = jsonObject.getString("errCode");
                String errmsg = jsonObject.getString("message");

                //0表示添加成功
                if (StringUtils.equals("200", errno)) {
                    kwCyphPush.setCode(BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage(errmsg);
                } else {
                    kwCyphPush.setCode(BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage(errno + "," + errmsg);
                }
            } else {
                kwCyphPush.setCode(BUSINESS_STATUS_OTHER);
            }
        } else {
            kwCyphPushLog.setBackUrl(hr.toString());
            kwCyphPushLog.setHttpCode(statusCode);
            kwCyphPushLog.setHttpContent(hr.toString());

            kwCyphPush.setCode(BUSINESS_STATUS_FAIL);
        }

        insert(kwCyphPush);
        kwCyphPushLog.setPushId(kwCyphPush.getId());
        kwCyphPushLogService.insert(kwCyphPushLog);

        return kwCyphPush.getCode();
    }

    private byte[] key_kaisen2 = hexStr2Bytes("60CA0469A02C49C1BE0C37FDD15B97AB");
    private byte[] iv_kaisen2 = hexStr2Bytes("A2B9438A3E1D48C8A457311D68FB2408");
    private final String key_sign_kaisen2 = "B55C0AE7A9FC445D978C0EE9F71F99CD";

    @Override
    public Integer applyKaisen2(String extId, String name, String appIdno, String appMobile, String product, String city
            , String cityCode, String sex, String age, String birthday, String province, String incomeMonth, String wagesType
            , String loan, String room, String car, String socialSecurity, String accumulationFund, String credit, String policy
            , String wld, String ip, String ipAddress, String creatTime, String remark1, String remark2, String userAgent
            , String remark3, String remark4, String remark5, String kstype) {
        String url = ConfigUtil.getConfig("kaisen2.url");

        JSONObject paramJsonObject = new JSONObject();

        paramJsonObject.put("name", name);
        paramJsonObject.put("tel", appMobile);
        paramJsonObject.put("sex", sex);
        paramJsonObject.put("birthday", birthday);
        paramJsonObject.put("age", age);
        paramJsonObject.put("cardNo", appIdno);
        paramJsonObject.put("province", province);
        paramJsonObject.put("city", city);
        paramJsonObject.put("adzoneId", "");
        paramJsonObject.put("question", "");
        paramJsonObject.put("attribute", "");
        paramJsonObject.put("portrait", "");
        paramJsonObject.put("remark", "");

        logger.info(paramJsonObject.toJSONString());

        String data = encrypt(paramJsonObject.toJSONString(), key_kaisen2, iv_kaisen2);

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("customers", data);
        paramMap.put("uid", "XB-LA-190327");
        paramMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
        String sign = signParams(paramMap, key_sign_kaisen2).toLowerCase();
        paramMap.put("signature", sign);
        logger.info(paramMap.toString());

        KwCyphPush kwCyphPush = new KwCyphPush();
        kwCyphPush.setInterfaceCode(CHANNEL_CODE_KAISEN2);
        kwCyphPush.setInterfaceName(CHANNEL_NAME_KAISEN2);
        kwCyphPush.setMobile(appMobile);

        KwCyphPushLog kwCyphPushLog = new KwCyphPushLog();
        kwCyphPushLog.setInterfaceCode(CHANNEL_CODE_KAISEN2);
        kwCyphPushLog.setInterfaceName(CHANNEL_NAME_KAISEN2);
        kwCyphPushLog.setMobile(appMobile);
        kwCyphPushLog.setSendUrl(url);
        kwCyphPushLog.setParams(paramMap.toString());

        HttpUtil.HttpResult hr = HttpUtil.doPost3(url, paramMap);
        int statusCode = hr.getStatusCode();
        if (statusCode == SC_OK) {
            kwCyphPushLog.setBackUrl(hr.toString());
            kwCyphPushLog.setHttpCode(SC_OK);
            kwCyphPushLog.setHttpContent(hr.getContent());

            String content = hr.getContent();
            if (!isEmpty(content)) {
                JSONObject jsonObject = (JSONObject) JSONObject.parse(content);

                String sucCode = jsonObject.getString("sucCode");
                String errmsg = jsonObject.getString("message");

                if (StringUtils.equals("1000", sucCode)) {
                    kwCyphPush.setCode(BUSINESS_STATUS_SUCCESS);
                    kwCyphPush.setMessage(errmsg);
                } else {
                    kwCyphPush.setCode(BUSINESS_STATUS_OTHER);
                    kwCyphPush.setMessage(sucCode + "," + errmsg);
                }
            } else {
                kwCyphPush.setCode(BUSINESS_STATUS_OTHER);
            }
        } else {
            kwCyphPushLog.setBackUrl(hr.toString());
            kwCyphPushLog.setHttpCode(statusCode);
            kwCyphPushLog.setHttpContent(hr.toString());

            kwCyphPush.setCode(BUSINESS_STATUS_FAIL);
        }

        insert(kwCyphPush);
        kwCyphPushLog.setPushId(kwCyphPush.getId());
        kwCyphPushLogService.insert(kwCyphPushLog);

        return kwCyphPush.getCode();
    }

    private String encrypt(String content, byte[] key, byte[] iv) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
            byte[] encrypted = cipher.doFinal(content.getBytes());
            return toHexString(encrypted).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 16进制字符串转字节
     */
    private byte[] hexStr2Bytes(String content) {
        if (content.length() < 1) {
            return null;
        }

        byte[] bytes = new byte[content.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high * 16 + low);
        }
        return bytes;
    }

    private String signParams(Map<String, String> paramMap, String keySign) {
        paramMap = deleteEmptyValue(paramMap);

        ArrayList<String> keys = new ArrayList<>(paramMap.keySet());
        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keys.size(); i++) {
            if (i == keys.size() - 1) {
                sb.append(keys.get(i) + "=" + paramMap.get(keys.get(i)));
            } else {
                sb.append(keys.get(i) + "=" + paramMap.get(keys.get(i)) + "&");
            }
        }

        String content = sb.toString() + keySign;

        try {
            return DigestUtils.md5Hex(content.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    private Map<String, String> deleteEmptyValue(Map<String, String> paramMap) {
        Map<String, String> newParamMap = new HashMap<>(paramMap);
        Set<String> deleteKeys = new HashSet<>();
        Set<String> allKeys = newParamMap.keySet();
        for (String key : allKeys) {
            if (isEmpty(newParamMap.get(key))) {
                deleteKeys.add(key);
            }
        }
        if (!deleteKeys.isEmpty()) {
            for (String key : deleteKeys) {
                newParamMap.remove(key);
            }
        }
        return newParamMap;
    }



}
